package com.weng.apistarter.crud.api;

import com.weng.apistarter.crud.entity.Role;
import com.weng.apistarter.crud.service.IRoleService;
import com.weng.apistarter.crud.model.vo.RoleVo;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.model.request.DeleteReqVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import org.apache.commons.lang3.StringUtils;
import com.weng.framework.web.base.BaseController;
import javax.validation.Valid;
import cn.hutool.core.convert.Convert;


/**
 * 角色 API实现
 *
 * @author weng
 * @date 2021-10-07 21:43:17
 *
 */  
@Slf4j
@RestController
@RequestMapping("/roles")
@Api(value = "角色api", tags = {"角色操作接口"})
public class RoleApi extends BaseController {
    @Autowired
    private IRoleService roleService;

    /**
     * 功能描述:单个获取角色
     */
    @ApiOperation(value = "获取角色", notes = "")
    @GetMapping(value = "/{id}")
    public ResponseData get(@PathVariable("id") String id){
        Role role = roleService.getById(id);
        return success(role);
    }


    /**
     * 条件 分页查询角色
     */
    @ApiOperation(value = "分页获取角色", notes = "")
    @GetMapping(value = "")
    public ResponseData list(Integer pageNo,Integer pageSize){
	IPage<Role> iPage = roleService.list(pageNo, pageSize);
        return success(iPage);
    }


    /**
	 * 新增角色
	 */
	@ApiOperation(value = "新增角色", notes = "")
	@PostMapping(value = "")
	public ResponseData add(@Valid @RequestBody RoleVo roleReq){
		Role role = Convert.convert(Role.class, roleReq);
		roleService.add(role);
		return success();
	}

	/**
	 * 更新角色
	 */
	@ApiOperation(value = "更新角色", notes = "")
	@PutMapping(value = "")
	public ResponseData update(@Valid @RequestBody RoleVo roleReq){
		Role role = Convert.convert(Role.class, roleReq);
		roleService.update(role);
		return success();
	}


	/**
	 * 删除角色
	 */
	@ApiOperation(value = "ids 删除角色", notes = "可以删除多个，中间以,号分隔")
	@DeleteMapping(value = "")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		roleService.removeByIds(Arrays.asList(ids.split(",")));
		return success(deleteReq.getIds());
	}
}
